package com.ajie.bluetoothcard.db;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.ajie.bluetoothcard.db.Column.Type;

public class DatabaseHelper extends SQLiteOpenHelper {
	public static final int VERSION = 1;
	public static final String DB_NAME = "bluetoothCard.db";
	List<Table> tables = new ArrayList<Table>();
	private static final String TAG = DatabaseHelper.class.getSimpleName();

	
	public DatabaseHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
	}
	
	public void addTable(Table table) {
        Log.d(TAG, "Adding table " + table.getName());
        tables.add(table);
    }

	@Override
	public void onCreate(SQLiteDatabase db) {
		initTable(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//		if(oldVersion == 3 && newVersion == 4){
//			//从版本1到版本2时，增加了一个字段 desc
//			String sql = "alter table ["+Table.ECGRecord+"] add ["+Column.RECORD_msgId+"]";
//			String sql2 = "alter table ["+Table.ECGRecord+"] add ["+Column.RECORD_averageHeartRate+"]";
//			String sql3 = "alter table ["+Table.ECGRecord+"] add ["+Column.RECORD_normalRange+"]";
//			String sql4 = "alter table ["+Table.ECGRecord+"] add ["+Column.RECORD_suspectedRisk+"]";
//			db.execSQL(sql);
//			db.execSQL(sql2);
//			db.execSQL(sql3);
//			db.execSQL(sql4);
//		}
	}

	private void initTable(SQLiteDatabase db) {

		String sqlCard = new Table(Table.Card)
		.addColumn(new IntegerColumn(Column.Card_ID).setAutoIncrement(true).setPrimaryKey(true))
		.addColumn(new Column(Column.Card_name,Type.STRING))
		.addColumn(new Column(Column.Card_mobilePhone,Type.STRING))
		.addColumn(new Column(Column.Card_telephone,Type.STRING))
		.addColumn(new Column(Column.Card_avatar,Type.STRING))
		.addColumn(new Column(Column.Card_type,Type.INTEGER))
		.addColumn(new Column(Column.Card_isDel,Type.INTEGER))
		.addColumn(new Column(Column.Card_title,Type.STRING))
		.addColumn(new Column(Column.Card_company,Type.STRING))
		.addColumn(new Column(Column.Card_voide,Type.STRING))
		.addColumn(new Column(Column.Card_address,Type.STRING))
		.addColumn(new Column(Column.Card_introduction,Type.STRING))
		.addColumn(new Column(Column.Card_time,Type.STRING))
		.addColumn(new Column(Column.Card_url,Type.STRING))
		.addColumn(new Column(Column.Card_email,Type.STRING))
		.addColumn(new Column(Column.Card_fax,Type.STRING))
		.getCreateString();
		Log.d("table","sqlMessage:"+sqlCard);
		db.execSQL(sqlCard);
		
		String sqlCardType = new Table(Table.Card_Type)
		.addColumn(new IntegerColumn(Column.Card_ID).setAutoIncrement(true).setPrimaryKey(true))
		.addColumn(new Column(Column.Card_Type_ID,Type.INTEGER))
		.addColumn(new Column(Column.Card_Type_name,Type.STRING))
		.addColumn(new Column(Column.Card_Type_number,Type.INTEGER))
		.getCreateString();
		Log.d("table","sqlMessage:"+sqlCardType);
		db.execSQL(sqlCardType);

		}
}
